package my.jieh.tips.web;

import java.util.*;
import java.util.regex.*;

public class REGEX {
  public static void main(String[] args) throws Exception {
    String xml = "<person name=\"ccc\">ccc@mail.km.kuas.edu.tw<tel>082313530</tel><tel>0938707315</tel></person>";
    System.out.println("split(xml, <>=)="+Arrays.asList(xml.split("[<>=]")));
    System.out.println("plaintext = "+xml2plaintext(xml));
    System.out.println("match = "+matchAt(xml, 0, "\\w+"));
    System.out.println("match = "+matchAt(xml, 0, "<\\w+"));
    System.out.println("matches = "+Arrays.asList(matches(xml, 1, "<tel>(\\w+)</tel>")));
    System.out.println("transform(xml, <(.*?)>, <(?1)>) = "+transform(xml, "<(.*?)>", "[(?1)]"));
    System.out.println("group = "+Arrays.asList(groups("3a+2b+1c=5f", "([\\w+\\+]+)=([\\w+\\+]+)")));
    System.out.println(transform(" xy abc w", "xy([^xy]*?)w", "<H1>(?1)</H1>"));
    System.out.println("matches:"+Arrays.asList(matches("...<!--wiki-->abc<!--/wiki-->...<!--wiki-->def<!--/wiki-->", 1, "(<!--wiki-->.*?<!--/wiki-->)")));
    System.out.println("transform(abc,def , (.*?),(.*?), (?1) <a>(?2)</a>) = "+transform("abc@def", "(.*?)@(.*)", "(?1) <a>(?2)</a>"));
  }

  public static String extract(String pText, String fromPat, String toPat) {
    StringBuffer rzText = new StringBuffer();
  Pattern p = Pattern.compile(fromPat);
  Matcher m = p.matcher(pText);
  int lastIdx = 0;
  while (m.find()) {
    String pat = toPat;
    for (int gi=1; gi<=m.groupCount(); gi++)
      pat = STR.replace(pat, "(?"+gi+")", m.group(gi));
    rzText.append(pat+"\n");
    lastIdx = m.end();
  }
  return rzText.toString();
  }

  public static String transform(String pText, String fromPat, String toPat) {
    StringBuffer rzText = new StringBuffer();
  Pattern p = Pattern.compile(fromPat);
  Matcher m = p.matcher(pText);
  int lastIdx = 0;
  while (m.find()) {
    rzText.append(pText.substring(lastIdx, m.start()));
    String pat = toPat;
    for (int gi=1; gi<=m.groupCount(); gi++)
      pat = STR.replace(pat, "(?"+gi+")", m.group(gi));
    rzText.append(pat);
    lastIdx = m.end();
  }
  rzText.append(pText.substring(lastIdx));
  return rzText.toString();
  }

  public static String matchAt(String pText, int pFrom, String pPattern) {
  Pattern p = Pattern.compile(pPattern, Pattern.CASE_INSENSITIVE|Pattern.DOTALL|Pattern.MULTILINE);
  Matcher m = p.matcher(pText.substring(pFrom));
  if (m.lookingAt()) return m.group(0);
  return null;
  }

  public static String[] matches(String pText, int pGroup, String pPattern) {
    Vector rzMatches = new Vector();
  Pattern p = Pattern.compile(pPattern, Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
  Matcher m = p.matcher(pText);
  while (m.find())
    rzMatches.add(m.group(pGroup));
  String[] rzStrs = new String[rzMatches.size()];
  for (int i=0; i<rzStrs.length; i++)
    rzStrs[i] = (String) rzMatches.get(i);
  return rzStrs;
  }

  public static String[] groups(String pText, String pPattern) {
  Pattern p = Pattern.compile(pPattern, Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
  Matcher m = p.matcher(pText);
  if (!m.matches()) return null;
    String[] rzGroups = new String[m.groupCount()];
  for (int i=0; i<m.groupCount();i++)
    rzGroups[i]=m.group(i+1);
  return rzGroups;
  }

  public static String xml2plaintext(String pXml) {
    StringBuffer plainText = new StringBuffer();
  String[] nodes = matches("<root>"+pXml+"</root>", 1, ">(.*?)<");
  for (int i=0; i<nodes.length; i++)
    plainText.append(nodes[i]+" ");
  return plainText.toString();
  }
}
/*
  public static String matchFirst(String pText, String pPattern) {
    String[] rzMatches = matches(pText, pPattern, 1);
    if (rzMatches.length == 1) return rzMatches[0];
    return null;
  }
*/
